/*
 *
 *  * Copyright (c) 2016. For Intelligent Group.
 *
 */

package com.intelligent.ispc.client.core.modal.spc;

import com.google.common.collect.Lists;
import com.intelligent.ispc.core.dto.TestItemDto;

import javax.swing.*;
import java.util.List;

/**
 * Created by Peter on 2016/9/6.
 */
/**
 * @param <T>
 */
public class TestItemListModel<T extends TestItemDto> extends AbstractListModel {

    private List<TestItemDto> data;

    /**
     * Constructor
     */
    public TestItemListModel() {
        data = Lists.newArrayList();
    }

    /**
     * Overwriter constructor
     *
     * @param dtos testItemDtos
     */
    public TestItemListModel(List<TestItemDto> dtos) {
        if (dtos == null) {
            data = Lists.newArrayList();
        }
        data = dtos;
    }

    @Override
    public int getSize() {
        return data.size();
    }

    @Override
    public Object getElementAt(int index) {
        if (data.get(index) == null) {
            return null;
        }
        return data.get(index).getName();
    }

    /**
     * Add element
     *
     * @param obj testItemDto
     */
    public void addElement(TestItemDto obj) {
        data.add(obj);
    }

    /**
     * Set data
     *
     * @param dtos testItemDtos
     */
    public void setData(List<TestItemDto> dtos) {
        if (dtos == null) {
            dtos = Lists.newArrayList();
        }
        data = dtos;
        fireContentsChanged(this, 0, data.size());
    }

    /**
     * Clean Data
     */
    public void cleanData() {
        data.clear();
    }
}